package com.kiln.juxingqs.ui

import android.view.View
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.base.PagerFooterAdapter
import client.global.commlib.comm_ui.draw.SpaceItemDecoration
import client.global.commlib.network.data.PagingJsonResult
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.PagingJydetailAdapter
import com.kiln.juxingqs.databinding.ActivityJydetailBinding
import com.kiln.juxingqs.ui.vm.JyDetailViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


class JyDetailActivity :
    BaseMiddleActivity<JyDetailViewModel, ActivityJydetailBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_jydetail
    }

    var isLoadMore = false
    val adapter: PagingJydetailAdapter by lazy {
        PagingJydetailAdapter().apply {
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }
            appendError = {
                isLoadMore = true
            }
        }
    }

    override fun initViewModel(): JyDetailViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(1))
        binding.recyclerView.adapter = adapter.withLoadStateFooter(PagerFooterAdapter())
        binding.recyclerView.onLoadMore = {
            if (isLoadMore)
                this@JyDetailActivity.adapter.retry()
            isLoadMore = false
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }
    }

    override fun initData() {
        var goodsId = intent.getStringExtra("goods_id")

        viewModel.viewModelScope.launch {
            viewModel.pagingDataAuto(50, hold = {
                val ret = viewModel.server.buy_deal_today(it, goodsId!!)
                PagingJsonResult(
                    list = ret.body()?.data?.buy_deal_list,
                    code = ret.body()?.code,
                    message = ret.body()?.message
                )
            }).collect {
                adapter.submitData(it)
            }
        }


    }

}